#include "error.h"
#include "utility.h"
#include "main_window.h"
#include "game_engine.h"

#include "AI/behavior/AIB_idle.h"
#include "building/bldg_ctzn_spawner.h"

int main(int args,char** argv){

    pthread_win32_process_attach_np();

    error::setFile("std_error.log");

    mwin = new main_window();
    mwin->create();

    util::fps_limiter limiter;

    bool done = false;
    MSG msg;

    game_engine::init();
    game_engine::addChar(new character(0,0,new AIB_idle()));
    game_engine::addChar(new character(0,0,new AIB_idle()));
    game_engine::addChar(new character(0,0,new AIB_idle()));
    game_engine::addChar(new character(0,0,new AIB_idle()));
    //game_engine::addBuild(new bldg_ctzn_spawner(30,21));
    game_engine::addTown(new town());

    while(!done){

        limiter.exec();

        while (PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
            if (msg.message==WM_QUIT) done = true;
            else{
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
        if( mwin ){
            mwin->actionExec();
            mwin->display();
        }
    }

    game_engine::release();
    error::end();
    pthread_win32_process_detach_np();

    return EXIT_SUCCESS;
}
